package cn.bone.mqtt.demo02;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * Created by MD-01 on 2017/4/21.
 */
public class ServerThread implements Runnable{

    private static Logger logger = LoggerFactory.getLogger(ServerThread.class);
    private MqttClient client;
    private MqttConnectOptions options;

    public ServerThread(MqttClient client,MqttConnectOptions options){
        this.client = client;
        this.options = options;
    }

    @Override
    public void run() {
        while(true){
            if(!client.isConnected()){
                logger.info("连接中断,尝试重连");
                try {
                    client.connect(options);
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }else{
                logger.info("服务端连接正常");
            }
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
